package com.ouc.pampas;
import com.google.common.base.Preconditions;
import javax.annotation.PostConstruct;

public class Setting
{
  private Mode mode;
  private String rootPath;
  private App implantApp;
  private String registryId;
  private boolean devMode;
  
  public Mode getMode()
  {
    return this.mode;
  }
  
  public void setMode(Mode mode)
  {
    this.mode = mode;
  }
  
  public String getRootPath()
  {
    return this.rootPath;
  }
  
  public void setRootPath(String rootPath)
  {
    this.rootPath = rootPath;
  }
  
  public App getImplantApp()
  {
    return this.implantApp;
  }
  
  public void setImplantApp(App implantApp)
  {
    this.implantApp = implantApp;
  }
  
  public String getRegistryId()
  {
    return this.registryId;
  }
  
  public void setRegistryId(String registryId)
  {
    this.registryId = registryId;
  }
  
  public Setting()
  {
    this.devMode = false;
  }
  
  public boolean isDevMode()
  {
    return this.devMode;
  }
  
  public void setDevMode(boolean devMode)
  {
    this.devMode = devMode;
  }
  
  @PostConstruct
  private void init()
  {
    switch(this.mode.ordinal())
    {
    case 1: 
      Preconditions.checkNotNull(this.implantApp, "implantApp in [Setting] should not be null when IMPLANT mode");
      Preconditions.checkNotNull(this.implantApp.getAssetsHome(), "assetsHome in implantApp should not be null");
      Preconditions.checkNotNull(this.implantApp.getConfigPath(), "configPath in implantApp should not be null");
      Preconditions.checkNotNull(this.implantApp.getKey(), "key in implantApp should not be null");
      this.implantApp.setAssetsHome(normalize(this.implantApp.getAssetsHome()));
      break;
    case 2: 
      Preconditions.checkNotNull(this.rootPath, "rootPath in [Setting] should not be null when CENTER mode");
      this.rootPath = normalize(this.rootPath);
    }
  }
  
  public static enum Mode
  {
    CENTER,  IMPLANT;
    
    private Mode() {}
  }
  
  private String normalize(String rootPath)
  {
    if (rootPath.endsWith("/")) {
      return rootPath;
    }
    return rootPath + "/";
  }
}
